package
{
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.FrameCheckControler;
	import Classes.GameUtils.GU_dealSharePreWin;
	import Classes.GameUtils.MenuVersion;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.GameViews.ViewPanels.VP_selectSex;
	import Classes.Interfaces.I_Main;
	import Classes.Managers.M_BaseCartoons;
	import Classes.Managers.M_CommonZombieCartoons;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.Managers.M_GameScenes;
	import Classes.Managers.M_LoadSWFSource;
	import Classes.Managers.M_PaperManCartoons;
	import Classes.Managers.M_Timer;
	import Classes.Managers.Manager;
	import Classes.staticinfo.PlatformInfo;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.SharedObject;
	import flash.system.Security;
	
	[SWF(backgroundColor=0x000000,width=760,height=600,frameRate="24")]
	public class ZombieTown extends Sprite implements I_Main
	{
		//		public static const MAINXMLPATH:String="configDatas/Main.XML"
		private static var _instance:ZombieTown;
		/**
		 * 用于清空缓存的参数 
		 */		
		public var pageParams:String="?0";
		private var _managersAry:Array;
		private var _mainXML:XML;
		private var _so:SharedObject;
		private var _showStage:Sprite;
		private var _otherShowSP:Sprite;
		private var _sysStage:Stage;
		private var param:Object;
		
		public function ZombieTown()
		{
			Security.allowDomain("*");
			_instance=this;
			this._showStage=new Sprite;
			this.addChild(this.showStage);
			
			this._otherShowSP=new Sprite;
			this.addChild(this.otherShowSP);
			
			addEventListener(Event.ADDED_TO_STAGE, onAddToStageHandler);
		}
		
		private function onAddToStageHandler(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddToStageHandler); 
			
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			_sysStage=this.stage;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyFun);
			
			new MenuVersion(this,"120714-12:00","1");
			
			//发布到现网上面 可以注释掉的
			this.locationGet();
			
		}
		
		private function keyFun(e:KeyboardEvent):void
		{
			VP_Main.instance.keyFun(e);
		}
		
		
		private function locationGet():void
		{
			/*this.so=SharedObject.getLocal("zombieTownShared");
			if(!this.so.data.visitNum)
			{
				this.so.data.visitNum=1;
			}
			else
			{
				this.so.data.visitNum+=1;
			}
			var visitTF:TextField=new TextField();
			visitTF.text="这是您第"+this.so.data.visitNum+"次访问游戏!";*/
			//			this.otherShowSP.addChild(visitTF);
			
			//加入空格键来控制帧频检测工具
			var frameObj:Object={"4a5028bc-383e-4add-9609-917f05b8f293":{"21001":{"serverTime":1324694678}},"b301fb93-0bf3-4a3e-a1b0-bbb5897c1987":{"21000":{"userID":25}}};
			var frameDis:DisplayObject= this.otherShowSP.addChild(new FrameCheckControler(frameObj));
			frameDis.visible=false;
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyFun);
			function keyFun(e:KeyboardEvent):void
			{
				if(e.keyCode==32)
				{
					frameDis.visible=!frameDis.visible;
				}
				if(M_ConfigData.instance.isOnLocalHost==0)
				{
					frameDis.visible=false;
				}
//				if(e.keyCode==87){
					
//					(GV_Map.instance.controlTarget as MMT_Character).autoRepairWall	=	true;
//				}
			}
		}
		
		public function locationShared():void
		{
			this.so.flush();
		}
		
		public function cleanShared():void
		{
			this.so.clear();
		}
		
		private function beginImport():void
		{
			if(this.managersAry.length)
			{
				var importManager:Manager=this.managersAry.shift();
				importManager.importData();
			}
			else
			{
				//				this.initOver();
				this.startLogin();
			}
		}
		
		public function dataImportOver(step:int=0):void
		{
			if(step>0)this.loadStep(step);
			this.beginImport();
		} 
		
		public var loadOverCallBackFun:Function;
		private var loadStep:Function;
		public var callloadProcessBarHandler:Function;
		public function initLogin(param:Object,_loadOverCallBackFun:Function,_loadStep:Function,pageParams:String,loadProcessBarHandler:Function):void
		{
			this.pageParams=pageParams;
			this.param=param;
			//			StyleManager.getInstance().uiDomain=_loadInfo.applicationDomain;
			this.loadOverCallBackFun=_loadOverCallBackFun;
			this.loadStep=_loadStep;
			this.callloadProcessBarHandler=loadProcessBarHandler;
			
			this._managersAry=
				[
					new M_ConfigData,
					new M_Communication,
					new M_Timer,
					new M_LoadSWFSource,
					new M_BaseCartoons,//1
					new M_CommonZombieCartoons,//2
					new M_PaperManCartoons,//3
					new M_GameScenes,
				];
			for each(var manager:Manager in this.managersAry)
			{
				manager.main=this;
			}
			this.beginImport();
			
		}
		
		public function startLogin():void
		{
			//给平台赋值
			PlatformInfo.instance.openkey=param.openkey;
			PlatformInfo.instance.pf=param.platform;//param.platform;
			PlatformInfo.instance.pfkey=param.pfkey;
			PlatformInfo.instance.openid=param.openid;
			
			
//			测试用的固定登陆账号
			if(M_ConfigData.instance.isOnLocalHost==1)PlatformInfo.instance.pf="pengyou";
			M_Communication.instance.send(11000,{"r":"r","s":"3a5c36ac29a033c9b9b5593f0be9f2f1","platform":"pengyou","t":1343028137828,"openid":"A96C27C390CA11779A28981B458183CF","friendId":"","k":"","pfkey":"dad8b517574057cf75047c420a0313d3","openkey":"4006F54EDFD289AE09D52CBA00D65624"});  
			//给别人调试的			
//			M_Communication.instance.send(11000,{"r":"r","openkey":"C6F21C47CE2853FFF5BE5648908D0693","s":"213fd95efc8ce1d5334c74dec1867a1e","k":"","t":1342860440968,"platform":"pengyou","pfkey":"cb010a72834f76a256c3b79fe03a33f6","openid":"DB7C265CC091A6283321121BFC74CAB7","friendId":""});
			
			//	现网获取页面的账号登陆
//			M_Communication.instance.send(11000,param);
		}
		
		//进入游戏
		public function enterGame():void
		{
			GD_Player.instance.creatMyHome();
		}
		
		//播放音乐
		public function playGameMusic():void
		{
			GD_Player.instance.playGameMusic();
		}
		
		/**
		 * 打开性别选择按钮 
		 * 
		 */		
		public function openSelectSexView():void
		{
			VP_selectSex.instance.openPanel();
		}
		
		/**
		 * feed回调方法 
		 * @param feedFunc
		 */		
		public function setFeedFunc(feedFunc:Function):void{
			M_ConfigData.instance.feedFunc = feedFunc;
		}
		

		/**
		 * 分享成功 
		 */		
		public function sendFeedOK():void
		{
			GU_dealSharePreWin.instance.sendShareCommand();
		}
		
		/**
		 * 购买成功 ( 在 打开Q点的平台)
		 * @param shopObj
		 * 
		 */		
		public function buyShopOk(itemId:String):void
		{
			M_Communication.instance.send(128014,{itemId:itemId});
		}
		
		
		/**
		 *显示Obj的字符形式 
		 * @param target
		 * @param layer
		 * @return 
		 */		
		public static function getObjStr(target:Object,layer:int=0):String
		{
			if((target is String) || (target is Number) || (target is Boolean))
			{
				return String(target);
			}
			else if(target==null)
			{
				return "null";
			}
			else
			{
				var returnStr:String="\n"+getSpaceStr(layer)+"{";
				for(var k:String in target)
				{
					returnStr+="\n"+getSpaceStr(layer+1)+k+":";
					returnStr+=getObjStr(target[k],layer+1);
					returnStr+=","
				}
				returnStr=returnStr.slice(0,returnStr.length-1);
				returnStr+="\n"+getSpaceStr(layer)+"}";
				return returnStr;
			}
			function getSpaceStr(layer:int):String
			{
				var spaceStr:String="";
				for(var i:int=0;i<layer;i++)
				{
					spaceStr+="  ";
				}
				return spaceStr;
			}
		}
		
		/*getter & setter*/
		public static function get instance():ZombieTown
		{
			return _instance;
		}
		
		public function get managersAry():Array
		{
			return _managersAry;
		}
		
		//		public function get mainXMLPath():String
		//		{
		//			return "";
		//		}
		
		public function get mainXML():XML
		{
			return _mainXML;
		}
		
		public function set mainXML(value:XML):void
		{
			_mainXML = value;
		}
		
		public function get showStage():DisplayObjectContainer
		{
			return _showStage;
		}
		
		public function get so():SharedObject
		{
			return _so;
		}
		
		public function set so(value:SharedObject):void
		{
			_so = value;
		}
		
		public function get otherShowSP():Sprite
		{
			return _otherShowSP;
		}
		
		public function get sysStage():Stage
		{
			return _sysStage;
		}
		
		
	}
}